|
|
|
| Table 16.4 Collection Object Built-In Properties and Methods | | Item | Task | | Count property | Returns the number of items in the collection | | Add method | Adds an object to the collection | | Item method | Gets a single object from the collection | | Remove method | Deletes an object from the collection |
|
|
|
|
|
|
The code in Listing 16.2 creates a new collection named colTextBoxes and adds all the text boxes on a form to the new collection. |
|
|
|
|
|
|
|
|
Listing 16.2 COLLECT.ZIPCreating a Collection with This Code |
|
|
|
|
|
|
|
|
Option Explicit
' Create a new collection to contain all the
' text boxes on a form
Dim colTextBoxes As New Collection
Private Sub Form_Initialize()
' Variable used in For Each to get controls.
Dim cntrlItem As Control
' Loop through the controls on the form.
For Each cntrlItem In Me.Controls
' If the control is a text box, add it to the
' collection of text boxes.
If TypeName(cntrlItem) = "TextBox" Then
colTextBoxes.Add cntrlItem
End If
Next cntrlItem
End Sub |
|
|
|
|
|
|
|
|
The code in Listing 16.3 uses the collection colTextBoxes to clear all the text entered on the form. |
|
|
|
|
|
|
|
|
Listing 16.3 Using For Each to Handle the Collection |
|
|
|
|
|
|
|
|
Sub cmdClear_Click()
' Variable used in For Each to get controls.
Dim cntrlItem As Control
' Clear each of the text boxes in the collection.
For Each cntrlItem In colTextBoxes
cntrlItem.Text = ""
Next cntrlItem
End Sub |
|
|
|
|
|
|
|
|
See "Using the ListView Control," p. 251 |
|
|
|
|
|